/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import ejb.Verwaltung;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Steffen
 */
@ManagedBean
@SessionScoped
public class Controller {
    private final static String START= "./index.xhtml?faces-redirect=true";
    private final static String REPLIES= "./forumanzeigen.xhtml?faces-redirect=true";
    private List<Forum> foren = new ArrayList() {};
    private Forum aktuell;
    @EJB
    private Verwaltung verwaltung;
    
    public Controller(){
        
    }

    public void createForum(Forum forum) {
        try {
            verwaltung.persist(forum);
        } catch (Exception ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void createReply(Reply r) {
        
        try {
            aktuell.addReply(r);
            verwaltung.persist(r);
            verwaltung.merge(aktuell);
        } catch (Exception ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
 
    public void setAktuell(Forum forum) {
        this.aktuell = forum;
    }

    public Forum getAktuell() {
        return this.aktuell;
    }
    
    public List getForen(){
        onPageLoad();
        return this.foren;
    }
    
    /**
     * @return the START
     */
    public String toStart() {
        
        return START;
    }

    /**
     * @return the TOPIC
     */
    public String toTopic(Forum f) {
        //Forum f = searchForForum(target);
        /*if(f!=null){
            this.aktuell = f;
        }*/
        this.aktuell = f;
        return REPLIES;
        
    }
    
    public void onPageLoad() {
        foren = verwaltung.getForen();
    }
    
    public String ausloggen(Nutzer nutzer) {
        ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
        ectx.invalidateSession();
        nutzer = null;
        return START;
    }
     
    
    
}